<?php 
namespace app\common\model;
class ShareStat extends Common{
	protected $insert = ['addtime'=>SYS_TIME];
	protected function getAddtimeAttr($value){
		return date("Y-m-d H:i", $value);
	}
    protected function getMinfoAttr($value, $data){
        return model('AdminUser')->where('userid', $data['userid'])->field('userid, realname, avatar')->find();
    }
    protected function getAlldataAttr($value, $data){
        $arr = [4=>'海报',5=>'文章'];
        $data['kindname'] = $arr[$data['kind']];
        $data['realname'] = model('AdminUser')->where('userid', $data['userid'])->value('realname');
        $data['dealer'] = model('AdminUser')->get_dealer($data['userid']);
        trace($data['realname']);
     
        return $data;
    }

     protected function getDealerAttr($value, $data){
      return model('AdminUser')->get_dealer($data['userid']);
    }

    //添加数据
    public function save_data($data){ 
        //allowField过滤post数组中的非数据表字段数据
        $result = $this->allowField(true)->isUpdate(false)->data($data)->save();
        if($result!==false){
            $pk = $this->getPk();
            $id = $this->$pk;
            return $id;
        }
        return false;
    }
	//增加数量
    public function add_log($userid,$kind,$nums=1){
        //每天排名
        $nowday = date("Y-m-d",SYS_TIME);
        $count_day = $this->where("userid = '$userid' And kind = '$kind' And addday = '$nowday' And typeid = 1")->count();
        if (!$count_day) {
            $data = array(
                'userid' => $userid,
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 1,
                'addday' => $nowday,
            );
            $this->save_data($data);
        }else{
            $this->where("userid = '$userid' And kind = '$kind' And addday = '$nowday' And typeid = 1")->setInc("nums", $nums);
        }

        //每周排名
        $nowweek = date("Y", SYS_TIME)."-".date('W')."-W";
        $count_week = $this->where("userid = '$userid' And kind = '$kind' And addday = '$nowweek'  And typeid = 2")->count();
        if (!$count_week) {
            $data = array(
                'userid' => $userid,
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 2,
                'addday' => $nowweek,
            );
            $this->save_data($data);
        }else{
            $this->where("userid = '$userid' And kind = '$kind' And addday = '$nowweek' And typeid = 2")->setInc("nums", $nums);
        }

        //每月排名
        $nowmou = date("Y-m",SYS_TIME);
        $count_mou = $this->where("userid = '$userid' And kind = '$kind' And addday = '$nowmou' And typeid = 3")->count();
        if (!$count_mou) {
            $data = array(
                'userid' => $userid,
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 3,
                'addday' => $nowmou,
            );
            $this->save_data($data);
        }else{
            $this->where("userid = '$userid' And kind = '$kind' And addday = '$nowmou' And typeid = 3")->setInc("nums", $nums);
        }
        //总排名
        $nowtime = date("Y-m-d",SYS_TIME);
        $count = $this->where("userid = '$userid' And kind = '$kind' And typeid = 4")->count();
        if (!$count) {
            $data = array(
                'userid' => $userid,
                'kind'  => $kind,
                'nums'   => $nums,
                'typeid' => 4,
                'addday' => $nowtime,
            );
            $this->save_data($data);
        }else{
            $this->where("userid = '$userid' And kind = '$kind' And typeid = 4")->setInc("nums", $nums);
        }
        return true;
    }
    public function list_datas($kind, $typeid){
        $where['kind'] = $kind;
        $where['typeid'] = $typeid;
        switch ($typeid) {
            case 1:
                $where['addday'] = date("Y-m-d",SYS_TIME);
                break;
            case 2:
                $where['addday'] = date("Y", SYS_TIME)."-".date('W')."-W";
                break;
            case 3:
                $where['addday'] = date("Y-m",SYS_TIME);
                break;
            case 4:
                // $where['addday'] = date("Y-m-d",SYS_TIME);
                break;
        }
        $list = $this->where($where)->order('nums desc, id asc')->select();
        $lists = [];
        foreach ($list as $v) {
            $v['minfo'] = $v['minfo'];
            $lists[] = $v;
        }
        return $lists;
    }
}